package cn.source.util;


import lombok.extern.slf4j.Slf4j;

import javax.validation.constraints.NotNull;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

@Slf4j
public class DateUtils {


    /**
     * 传入两个时间,计算出两个时间之间的时长 （天数）
     * @param startTime     开始时间
     * @param endTime       结束时间
     * @return
     */
    public static int timeDistanceDay(@NotNull Date startTime,Date endTime){
        Calendar startCalendar = Calendar.getInstance();
        Calendar endCalendar = Calendar.getInstance();
        startCalendar.setTime(startTime);
        endCalendar.setTime(endTime);
        long millisecond =  endCalendar.getTimeInMillis() - startCalendar.getTimeInMillis();
        double day = millisecond / 60000.0 / 60.0 / 24.0;
        return  (int)Math.floor(day);
    }

    /**
     * 传入两个时间,计算出两个时间之间的时长 （小时）
     * @param startTime     开始时间
     * @param endTime       结束时间
     * @return
     */
    public static int timeDistanceHour(@NotNull Date startTime,Date endTime){
        Calendar startCalendar = Calendar.getInstance();
        Calendar endCalendar = Calendar.getInstance();
        startCalendar.setTime(startTime);
        endCalendar.setTime(endTime);
        long millisecond = endCalendar.getTimeInMillis() - startCalendar.getTimeInMillis();
        double hour = millisecond / 60000.0 / 60.0;
        return (int) Math.floor(hour);
    }

    /**
     * 传入两个时间,计算出两个时间之间的时长 （分钟）
     * @param startTime     开始时间
     * @param endTime       结束时间
     * @return
     */
    public static int timeDistanceMinute(@NotNull Date startTime,Date endTime){
        Calendar startCalendar = Calendar.getInstance();
        Calendar endCalendar = Calendar.getInstance();
        startCalendar.setTime(startTime);
        endCalendar.setTime(endTime);
        long millisecond = endCalendar.getTimeInMillis() - startCalendar.getTimeInMillis();
        double minute = millisecond / 60000.0;
        return (int) Math.floor(minute);
    }

    /**
     * 传入两个时间,计算出两个时间之间的时长 （秒）
     * @param startTime     开始时间
     * @param endTime       结束时间
     * @return
     */
    public static int timeDistanceSecond(@NotNull Date startTime,Date endTime){
        Calendar startCalendar = Calendar.getInstance();
        Calendar endCalendar = Calendar.getInstance();
        startCalendar.setTime(startTime);
        endCalendar.setTime(endTime);
        long millisecond = endCalendar.getTimeInMillis() - startCalendar.getTimeInMillis();
        double second = millisecond / 1000.0;
        return (int) Math.floor(second);
    }

    /**
     * 查询n秒后的时间
     * @param date
     * @param seconds
     * @return
     */
    public static Date addSeconds(@NotNull Date date, int seconds) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.SECOND, seconds);
        return calendar.getTime();
    }

    /**
     * 查询任意时间 前后的时间
     * @param date
     * @param field
     * @param count
     * @return
     */
    public static Date addArbitrarilyTime(@NotNull Date date,Integer field ,int count) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(field,count);
        return calendar.getTime();
    }

    public static Date dateToDayStart(@NotNull Date date){
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        instance.set(Calendar.HOUR_OF_DAY,0);
        instance.set(Calendar.MINUTE,0);
        instance.set(Calendar.SECOND,0);
        instance.set(Calendar.MILLISECOND,0);
        return instance.getTime();
    }

    public static Date dateToDayEnd(@NotNull Date date){
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        instance.set(Calendar.HOUR_OF_DAY,23);
        instance.set(Calendar.MINUTE,59);
        instance.set(Calendar.SECOND,59);
        instance.set(Calendar.MILLISECOND,999);
        return instance.getTime();
    }

    /**
     * 当月开始一刻
     * @param date
     * @return
     */
    public static Date dateMonthStart(@NotNull Date date){
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        instance.set(Calendar.DAY_OF_MONTH,1);
        return dateToDayStart(instance.getTime());
    }

    /**
     * 当月最后一刻
     * @param date
     * @return
     */
    public static Date dateMonthEnd(@NotNull Date date){
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        instance.set(Calendar.DAY_OF_MONTH,instance.getActualMaximum(Calendar.DAY_OF_MONTH));
        return dateToDayEnd(instance.getTime());
    }

    /**
     * 比较时间 返回分钟
     * @param time1
     * @param time2
     * @return
     */
    public static long compare(@NotNull Date time1,@NotNull Date time2){
        return TimeUnit.MINUTES.convert(time1.getTime()-time2.getTime(),TimeUnit.MILLISECONDS);
    }

    /*public static void main(String[] args) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date now = new Date();
        System.out.println("当前时间：" + dateFormat.format(now));

        Date newDate = addSeconds(now, 60);
        System.out.println("60秒后：" + dateFormat.format(newDate));
    }*/
    /**
     * 自定义格式
     * @param time 时间
     * @param pattern  格式
     * @return
     */
    public static String timeFormat(@NotNull Date time,String pattern){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        return simpleDateFormat.format(time);
    }

    /**
     * 短日期格式 ：yyyy-MM-dd
     * @param time 时间
     * @return
     */
    public static String timeFormatShort(@NotNull Date time){
        return timeFormat(time,"yyyy-MM-dd");
    }

    /**
     * 长日期格式 ：yyyy-MM-dd HH:mm:ss
     * @param time 时间
     * @return
     */
    public static String timeFormatLong(Date time){
        return timeFormat(time,"yyyy-MM-dd HH:mm:ss");
    }


    public static Date rfc3339ToDate(String rfc3339String){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX", Locale.CHINA);
        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
        try {
            return sdf.parse(rfc3339String);
        } catch (ParseException e) {
            log.error("rfc3339转Date失败：{}",rfc3339String,e);
            return new Date();
        }
    }

    public static Date stringParseTime(String time,String pattern){
        try {
            return new SimpleDateFormat(pattern).parse(time);
        }catch (Exception e){
            return null;
        }
    }

}
